package com.py.framework.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.py.framework.core.common.Page;
import com.py.framework.service.ActDefinedService;

/**
 * 流程定义管理Service实现
 * @author Leegern
 * @date   2018年5月22日
 */
@Service
public class ActDefinedServiceImpl implements ActDefinedService {
	
	@Autowired
	private RepositoryService repositoryService;
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.ActDefinedService#queryActDefinedDataList(com.py.framework.core.common.Page, java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> queryActDefinedDataList(Page page, String actKey) {
		List<Map<String, Object>> result = new ArrayList<>();
		Map<String, Object> item = null;
		Deployment deployment = null;
		String deploymentId = "";
		
		int firstResult = (page.getCurrentPage() - 1) * page.getPageSize();
		ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery()
				.orderByProcessDefinitionKey().desc();
		// 是否有查询条件
		if (StringUtils.isNotBlank(actKey)) {
			StringBuffer sb = new StringBuffer();
            sb.append("%")
              .append(actKey)
              .append("%");
			processDefinitionQuery = processDefinitionQuery.processDefinitionKeyLike(sb.toString());
		}
		// 查询最新版本
		processDefinitionQuery = processDefinitionQuery.latestVersion();
		// 查询数据
		List<ProcessDefinition> datas = processDefinitionQuery.listPage(firstResult, page.getPageSize());
		if (! CollectionUtils.isEmpty(datas)) {
			for (ProcessDefinition processDefinition : datas) {
				item = new HashMap<>();
				item.put("processName", processDefinition.getName());      // 流程名称
				item.put("processKey", processDefinition.getKey());        // 键
				item.put("processVersion", processDefinition.getVersion());// 版本号

				deploymentId = processDefinition.getDeploymentId();
				item.put("deploymentId", deploymentId);                    // 序号
				if (StringUtils.isNotBlank(deploymentId)) {
					deployment = repositoryService.createDeploymentQuery().deploymentId(deploymentId).singleResult();
					if (null != deployment) {
						item.put("deploymentName", deployment.getName());           // 模型名称
						item.put("deploymentTime", deployment.getDeploymentTime()); // 发布时间
					}
				}
				result.add(item);
			}
		}
		page.setTotalCount((int) processDefinitionQuery.count());
		return result;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.py.framework.service.ActDefinedService#delActDefined(java.lang.String)
	 */
	@Override
	public int delActDefined(String actKey) {
		if (StringUtils.isNotBlank(actKey)) {
			List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
					.processDefinitionKey(actKey).list();
			if (! CollectionUtils.isEmpty(list)) {
				for (ProcessDefinition processDefinition : list) {
					// 级联删除,即在流程实例在运行活动中也要删除 (可能会影响到历史查询功能)
					repositoryService.deleteDeployment(processDefinition.getDeploymentId(), true);
				}
				return list.size();
			}
		}
		return 0;
	}
}